<template>
  <div class="wrap" id="children3">
    <el-dialog
      title="修改绑定手机号"
      :visible.sync="state"
      @close="close"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @open="open"
    >
      <p class="tss" v-if="userInfo">更换后,下次登录可使用新手机号码登陆。当前手机号:{{phone}}</p>
      <el-form>
        <el-form-item label="输入新手机号" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="tel"></el-input>
        </el-form-item>
        <el-form-item label="输入手机验证码" :label-width="formLabelWidth">
          <div class="sendCode">
            <el-input autocomplete="off" v-model="checkCode"></el-input>
            <el-button type="primary" @click="getCode">{{ms}}</el-button>
          </div>
        </el-form-item>
        <el-form-item label="输入登陆密码" :label-width="formLabelWidth">
          <el-input autocomplete="off" show-password v-model="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="close(1)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { sendSms, chief } from "../utils/interface";
export default {
  props: {
    phoneState: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLabelWidth: "120px",
      userInfo: null,
      phone: "",
      tel: "",
      checkCode: "",
      password: "",
      bok: true,
      ms: "获取验证码"
    };
  },
  computed: {
    state: {
      get() {
        return this.phoneState;
      },
      set() {}
    }
  },
  methods: {
    close(v) {
      if (v) {
        if (!/^1[3456789]\d{9}$/.test(this.tel)) {
          this.$alert("手机号码有误，请重填", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.checkCode) {
          this.$alert("请输入验证码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.password) {
          this.$alert("请输入密码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        this.$emit("getPhone", {
          tel: this.tel,
          checkCode: this.checkCode,
          password: this.password
        });
      } else {
        this.$emit("getPhone");
      }
    },
    getCode() {
      if (!/^1[3456789]\d{9}$/.test(this.tel)) {
        this.$alert("手机号码有误，请重填", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      if (!this.bok) {
        return false;
      }
      chief({ account: this.tel }).then(res => {
        if (res.data.data >= 1) {
          this.$confirm("该账号已存在请直接登陆", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          });
          return false;
        } else {
          this.bok = false;
          let n = 60;
          sendSms({ account: this.tel }).then(res => {
            let timer = setInterval(() => {
              this.ms = `${n}`;
              n--;
              if (n <= 0) {
                this.bok = true;
                this.ms = "重新获取验证码";
                n = 60;
                clearInterval(timer);
              }
            }, 1000);
          });
        }
      });
    },
    open() {
      this.phone = "";
      this.tel = "";
      this.checkCode = "";
      this.password = "";
      this.bok = true;
      this.ms = "获取验证码";
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));

      this.phone =
        this.userInfo.account.substr(0, 3) +
        "****" +
        this.userInfo.account.substr(7);
      console.log(this.phone);
    }
  },
  mounted() {}
};
</script>
<style lang="scss" scoped>
.tss {
  font-size: 12px;
  text-align: center;
  margin-bottom: 30px;
  color: #f56c6c;
}
.el-input {
  max-width: 330px;
}
.el-button {
  max-width: 134px;
}
.sendCode {
  display: flex;
  align-items: center;
  .el-input {
    width: auto;
    margin-right: 10px;
  }
}
</style>
<style lang="scss">
#children3 .el-dialog {
  width: 30% !important;
  min-width: 400px;
}
</style>


